/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rjp.map;

import gnu.trove.map.hash.TIntFloatHashMap;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author mwh
 */
public class ArraySantaMap {

    float[] xs;

    float[] ys;

    final float maxX;

    final float maxY;

    final float minX;

    final float minY;

    public ArraySantaMap(String fn) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(fn));
        String line;
        reader.readLine();
        int maxIndex = Integer.MIN_VALUE;
        while ((line = reader.readLine()) != null) {
            String[] fields = line.split(",");
            maxIndex = Math.max(maxIndex, Integer.parseInt(fields[0]));
        }
        reader.close();
        xs = new float[maxIndex + 1];
        ys = new float[maxIndex + 1];


        reader = new BufferedReader(new FileReader(fn));

        reader.readLine();
        float maX = Float.NEGATIVE_INFINITY;
        float maY = Float.NEGATIVE_INFINITY;
        float miX = Float.POSITIVE_INFINITY;
        float miY = Float.POSITIVE_INFINITY;
        while ((line = reader.readLine()) != null) {
            String[] fields = line.split(",");
            int index = Integer.parseInt(fields[0]);
            float x = Float.parseFloat(fields[1]);
            float y = Float.parseFloat(fields[2]);
            xs[index] = x;
            ys[index] = y;


            maX = Math.max(maX, x);
            maY = Math.max(maY, y);
            miX = Math.min(miX, x);
            miY = Math.min(miY, y);

        }
        maxX = maX;
        maxY = maY;
        minX = miX;
        minY = miX;
        reader.close();
    }
    
     public float getDistance(int indexA, int indexB) {
    	if(indexA==indexB)
    	{
    		return 0;
    	}
        float xdist = xs[indexA] - xs[indexB];
        float ydist = ys[indexA] - ys[indexB];

        return (float) Math.sqrt(xdist * xdist + ydist * ydist);
    }
    

    public float getPathLength(List<Integer> path) {
        float l = 0;
        for (int i=1; i<path.size(); i++) {
            int indexA = path.get(i-1);
            int indexB = path.get(i);
            l = l + getDistance(indexA, indexB);
        }
        return l;
    }


    public int size() {
        return xs.length;
    }
   
}
